package com.zhouwr.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhouwr.common.core.aspect.annotation.AutoLog;
import com.zhouwr.common.core.vo.Query;
import com.zhouwr.common.core.vo.Result;
import com.zhouwr.system.entity.UserEntity;
import com.zhouwr.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 系统用户
 *
 * @author zhouwr
 * @email 1551541860@qq.com
 * @date 2020-10-08 15:53:25
 */
@Slf4j
@Api(tags = "系统用户")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 分页列表查询-系统用户
     *
     * @param params 分页查询参数
     * @return Result<IPage < UserEntity>>
     */
    @AutoLog(value = "分页列表查询-系统用户")
    @ApiOperation(value = "分页列表查询-系统用户" , notes = "分页列表查询-系统用户")
    @RequestMapping("/list")
    // @RequiresPermissions("system:user:list")
    public Result<IPage<UserEntity>> list(@RequestParam Map<String, Object> params) {
        IPage<UserEntity> page = userService.page(new Query<UserEntity>().getPage(params), new QueryWrapper<UserEntity>());
        return Result.ok(page, "分页查询成功！");
    }

    @RequestMapping("/login")
    public Result<?> login() {
        // code: 20000,
        // data: token
        JSONObject data = new JSONObject();
        data.put("token", "admin-token");
        return Result.ok(data, 200);
    }

    @RequestMapping("/info")
    public Result<?> info() {
        // code: 20000,
        // data: token
        JSONObject data = new JSONObject();
        data.put("roles", "admin,editer".split(","));
        data.put("introduction", "I am a super administrator");
        data.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        data.put("name", "Super Admin");
        return Result.ok(data, 200);
    }

    /**
     * 根据id获取-系统用户
     *
     * @param id
     * @return Result<UserEntity>
     */
    @AutoLog(value = "根据id获取-系统用户")
    @ApiOperation(value = "根据id获取-系统用户" , notes = "根据id获取-系统用户")
    @RequestMapping("/get/{id}")
    // @RequiresPermissions("system:user:info")
    public Result<UserEntity> info(@PathVariable("id") String id) {
        UserEntity user = userService.getById(id);
        return Result.ok(user, "系统用户" + "信息获取" + (user != null ? "成功" : "失败"));
    }

    /**
     * 保存-系统用户
     *
     * @param user
     * @return Result<?>
     */
    @AutoLog(value = "保存-系统用户")
    @ApiOperation(value = "保存-系统用户" , notes = "保存-系统用户")
    @RequestMapping("/save")
    // @RequiresPermissions("system:user:save")
    public Result<?> save(@RequestBody UserEntity user) {
        boolean b = userService.save(user);
        return Result.ok(b, "系统用户" + "信息保存" + (b ? "成功" : "失败"));
    }

    /**
     * 修改-系统用户
     *
     * @param user
     * @return Result</ ?>
     */
    @AutoLog(value = "修改-系统用户")
    @ApiOperation(value = "修改-系统用户" , notes = "修改-系统用户")
    @RequestMapping("/update")
    // @RequiresPermissions("system:user:update")
    public Result<?> update(@RequestBody UserEntity user) {
        boolean b = userService.updateById(user);
        return Result.ok(b, "系统用户" + "信息修改" + (b ? "成功" : "失败"));
    }

    /**
     * 删除-系统用户
     *
     * @param ids 主键数组
     * @return Result<?>
     */
    @AutoLog(value = "删除-系统用户")
    @ApiOperation(value = "删除-系统用户" , notes = "删除-系统用户")
    @RequestMapping("/delete")
    // @RequiresPermissions("system:user:delete")
    public Result<?> delete(@RequestBody String[] ids) {
        boolean b = userService.removeByIds(Arrays.asList(ids));
        return Result.ok(b, "系统用户" + "信息删除" + (b ? "成功" : "失败"));
    }

}
